<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 
 * 后台菜单控制器
 * @author 66diqiu-li
 *
 */
class Admin_Adminmenu_module extends CI_Module {
	

    /**
     * 站点配置ID
     * @var string
     */
    private $sid = '';
	/**
	 * 
	 * 构造函数
	 */
    public function __construct(){
    	parent::__construct();
        $this->sid = $GLOBALS['sid'] ;
    	$this->load->model('admin.Menu_model');
    }
	
	/**
	 * 
	 * 菜单列表
	 */
    function index(){
    	//引入树形类
        $this->load->library('mytreeclass');
        $list = $this->Menu_model->get_list();

        $res = array();
        foreach($list as $key=>$val){
            if($val['parent_id']==0){
                $res[$key] = $val;
                $res[$key]['list'] =array();
                unset($list[$key]);
                foreach($list as $k1=>$v1){
                    if($v1['parent_id']==$val['id']){
                        $res[$key]['list'][$k1]=$v1;
                        $res[$key]['list'][$k1]['list'] = array();
                        unset($list[$k1]);
                        foreach($list as $k2=>$v2){
                            if($v2['parent_id']==$v1['id']){
                                array_push($res[$key]['list'][$k1]['list'], $v2);
                                unset($list[$k2]);
                            }
                        }
                    }
                }
            }
        }
        /*
        $this->mytreeclass->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $this->mytreeclass->nbsp = '&nbsp;&nbsp;&nbsp;';
        $this->mytreeclass->init($result);
        $str = "<tr><td class='btn_min'>
		            <div class='operat hidden'>
		                <a class='icon-cog action' href='javascript:;'>处理</a>
		                <div class='menu_select'>
		                    <ul>
		                        <li><a class='icon-pencil' href='".site_url('admin/adminmenu/edit?id=$id')."'>编辑</a></li>
		                        <li><a class='icon-remove-2 doDel' href='javascript:;' data-uri='".site_url('admin/adminmenu/ajax_remove?id=$id')."'>删除</a></li>
		                    </ul>
		                </div>
		            </div>
		        </td><td>\$spacer\$name</td><td>\$app/\$controller/\$action</td><td><img class='pointer' data-id='\$id' style='cursor: pointer;' data-field='status' data-value='\$status' src='".URLPRE."static/admin/images/icons/icon_\$status.png' /></td></tr>";
        $select_categorys = $this->mytreeclass->get_tree(0, $str);        
        $data['select_categorys'] = $select_categorys;*/
        $data['res'] = $res;//print_r($data['res']);
        $this->load->view('adminmenu/adminmenu_index2',$data);
    }
    
    /**
     * 
     * 菜单添加页面
     */
    function add(){
    	
    	//引入树形类
        $this->load->library('mytreeclass');
        $parent_id = 0;
        $result = $this->Menu_model->get_list();
        $array = array();
        foreach($result as $r){
            $r['selected'] = $r['id']==$parent_id ? 'selected':'';
            $array[] = $r;
        }      
        $this->mytreeclass->init($array);     
        $str = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $select_categorys = $this->mytreeclass->get_tree(0, $str);     
        $data['select_categorys'] = $select_categorys;
        
    	//数据保存URL
        $data['form_post'] = site_url('admin/adminmenu/save');
        $this->load->view('adminmenu/adminmenu_add',$data);
    }
    
    /**
     * 
     * 菜单编辑页面
     */
    function edit(){    	
    	$id = $this->input->get('id');
    	//引入树形类
        $this->load->library('mytreeclass');
    	$row = $this->Menu_model->get_row(array('id'=>$id));
        $parent_id = $row['parent_id'];
        $result = $this->Menu_model->get_list();
        $array = array();
        foreach($result as $r){
            $r['selected'] = $r['id']==$parent_id ? 'selected':'';
            $array[] = $r;
        }      
        $this->mytreeclass->init($array);     
        $str = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $select_categorys = $this->mytreeclass->get_tree(0, $str);     
        $data['select_categorys'] = $select_categorys;
        $data['row'] = $row;
    	//数据保存URL
        $data['form_post'] = site_url('admin/adminmenu/save');
        $this->load->view('adminmenu/adminmenu_edit',$data);
    }
    
    /**
     * 
     * 菜单保存操作
     */
    function save(){
        $data = $this->input->post('Form');
        $id = $this->input->post('id');
        if(empty($id)){
        	//操作日志
        	$log = array('action' => 'add');
            $res = $this->Menu_model->add_row($data);
        }else{
        	//操作日志
        	$log = array('action' => 'edit');
            $res = $this->Menu_model->edit_row($data,array('id'=>$id));
        }
        if($res){
        	
        	//操作日志添加
        	$log['log'] = '后台管理菜单-'.$data['name'];
        	$this->_record_log($log);
        	
            $this->success('操作成功',site_url('admin/adminmenu/index'),true);
        }else{
        	$this->error('操作失败',site_url('admin/adminmenu/index'),true);
        }
    }
    
    /**
     * 
     * 删除操作
     */
    function ajax_remove(){
        $id = $this->input->get('id');
        
        //判断是否有子菜单
        $is_has_child = $this->Menu_model->is_has_child($id);
        if($is_has_child){
            $this->error('该菜单存在子菜单,请先删除其子菜单!','',true);
        }
        //查询菜单名称
        $row = $this->Menu_model->get_row(array('id'=>$id),'name');
        $res = $this->Menu_model->remove_row(array('id'=>$id));
        if($res){
        	//操作日志
        	$log = array('action'=>'remove','log'=>'后台管理菜单('.$row['name'].')');
        	$this->_record_log($log);
            $this->success('操作成功','',true);
        }else{
        	$this->error('操作失败','',true);
        }
    }
    
    /**
     * 
     * 修改状态信息
     */
    function ajax_status(){
	    $post = $this->input->post();
        $data[$post['field']] = $post['val'];
        
        $res = $this->Menu_model->edit_row($data,array('id'=>$post['id']));
        if($res){
            //操作日志
            $row = $this->Menu_model->get_row(array('id'=>$post['id']),'name');
            $log = array('action'=>'edit','log'=>'后台管理菜单('.$row['name'].')-状态值');
            $this->_record_log($log);
            
            $this->success('操作成功','',true);
        }else{
        	$this->error('操作失败','',true);
        }
    }
    /**
     *
     * 修改状态信息
     */
    function edit_sort(){
        $id = $this->input->get_post('id');
        $sort_order = $this->input->get_post('sort_order');
        $res = $this->Menu_model->edit_row(array('sort_order'=>$sort_order),array('id'=>$id));
        if($res){
            echo 1;exit;
        }else{
            echo 0;exit;
        }
    }
    /**
     *
     * 站点菜单列表
     */
    function site_index(){
        //引入树形类
        $this->load->library('mytreeclass');
        $result = $this->Menu_model->get_list();
        $this->mytreeclass->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $this->mytreeclass->nbsp = '&nbsp;&nbsp;&nbsp;';
        $this->mytreeclass->init($result);
        $str = "<tr><td>\$spacer\$name</td><td>\$app/\$controller/\$action</td></tr>";
        $select_categorys = $this->mytreeclass->get_tree(0, $str);
        $data['select_categorys'] = $select_categorys;
        $this->load->view('adminmenu/adminmenu_site_index',$data);
    }

}